/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package it.mdc.hearts.war.room;

import it.mdc.hearts.war.room.core.GameTable;
import java.io.Serializable;
import java.util.UUID;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.RequestScoped;

/**
 *
 * @author Administrator
 */

@ManagedBean(name = "tableManagerBean")
@RequestScoped
public class TableManagerBean implements Serializable{
    
    @ManagedProperty(value = "#{mainRoomBean}") // MY SESSION BEAN
    private MainRoomBean room;
    @ManagedProperty(value = "#{sessionUserBean}") // MY SESSION BEAN
    private SessionUserBean userSession;
    
    private String name;
    private Integer aiPlayer;
    
    private GameTable selectedTable;

    public MainRoomBean getRoom() {
        return room;
    }

    public void setRoom(MainRoomBean room) {
        this.room = room;
    }

    public SessionUserBean getUserSession() {
        return userSession;
    }

    public void setUserSession(SessionUserBean userSession) {
        this.userSession = userSession;
    }

    public GameTable getSelectedTable() {
        return selectedTable;
    }

    public void setSelectedTable(GameTable selectedTable) {
        this.selectedTable = selectedTable;
    }
        
     public String getName() {
        return name;
    }
    
    public void setName(String name) {
        this.name = name;
    }

    public Integer getAiPlayer() {
        return aiPlayer;
    }

    public void setAiPlayer(Integer aiPlayer) {
        this.aiPlayer = aiPlayer;
    }
    
    public String addAndJoinTable()
    {        
        UUID tableUUID = UUID.randomUUID();
        Integer userID = userSession.getID();
        
        GameTable newGameTable = new GameTable(userID);
        newGameTable.setName(name);
        newGameTable.setAiPlayer(aiPlayer);
        newGameTable.setTableKey(tableUUID); 
        
        if(!newGameTable.setPlayer(userSession.getUserInfo()))
        {
            return null;
        }
        
        room.addGameTable(tableUUID, newGameTable);
        userSession.getUserInfo().setCurrentTable(newGameTable);
                
        return "joinTable";
    }
    
     public String joinTable()
    { 
        if(selectedTable == null)
        {
            return null;
        }
        
        if(!selectedTable.setPlayer(userSession.getUserInfo()))
        {
            return null;
        }
        
        userSession.getUserInfo().setCurrentTable(selectedTable);
        
        return "joinTable";
    }
     
    public String watchTable()
    {
        if(selectedTable == null)
        {
            return null;
        }
        
        userSession.getUserInfo().setCurrentTable(selectedTable);
        selectedTable.addViewer(userSession.getUserInfo());
        return "joinTable";
    }
    
    public String exitTable()
    {
        if(!userSession.isJoinedInTable())
        {
            return "exitTable";
        }
        
        GameTable currentTable = userSession.getUserInfo().getCurrentTable();
        Integer userID = userSession.getID();
        
        if(userID == currentTable.getOwnerID())
        {
            UUID tableUUID = currentTable.getTableKey();
            room.removeGameTable(tableUUID);
        }
        
        if(currentTable.isPlayer(userID))
        {
            currentTable.removePlayer(userSession.getUserInfo());
        }
        else
        {
            currentTable.removeViewer(userSession.getUserInfo());
        }
        
        userSession.getUserInfo().setCurrentTable(null);

        return "exitTable";
    }
}